//
//  LanguageViewController.m
//  ChartsDemo
//
//  Created by wusiping on 2021/4/1.
//

#import "DeviceManagerVC.h"
#import "LanguageTableViewCell.h"
#import "ARTConfig.h"
#import "ARTDeviceListCell.h"
#import "ARTWatchSettingController.h"

@interface DeviceManagerVC ()<UITableViewDelegate,UITableViewDataSource,ARTDeviceListCellDelegate>

@property (nonatomic,strong) UILabel *tipsLab;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) UIImageView *icon;

@end

@implementation DeviceManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataConfig];
    [self UIConfig];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tableView reloadData];
}

#pragma mark - dataConfig
- (void)dataConfig
{
   
    self.dataArray = [NSUserDefaults jk_arrayForKey:ART_Storage_DeviceList];
}

#pragma mark - UIConfig
- (void)UIConfig{
    self.view.backgroundColor = J_ColorFromHEX(0xF6F6F6);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(-10, 0, 47, 27);
    [backBtn setImage:[UIImage imageNamed:@"i_return_black"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
     self.navigationItem.leftBarButtonItem = backItem;
//    [self.view setBackgroundColor:[UIColor jk_colorWithHexString:@"#FBFBFB"]];
    
    [self setTitle:FYLocalizedString(@"my_device", @"")];
//    [self.view setBackgroundColor:[UIColor jk_colorWithHexString:@"#FBFBFB"]];
 
    self.tableView = [[UITableView alloc] init];
    [self.tableView setBackgroundColor:J_ColorFromHEX(0xF6F6F6)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.separatorColor = [UIColor colorWithString:@"#F1F1F1"];
//    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0 );
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
//    [self.tableView registerClass:[LanguageTableViewCell class] forCellReuseIdentifier:[LanguageTableViewCell jk_className]];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(4);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(0);
    }];
    self.tableView.layer.cornerRadius = 10;
    self.tableView.layer.masksToBounds = YES;
    
    
    
    self.icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_no_data"]];
    self.icon.hidden = YES;
    [self.tableView addSubview:self.icon];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
    
    self.tipsLab = [[UILabel alloc] init];
    self.tipsLab.text = FYLocalizedString(@"no_data", @"");
    self.tipsLab.textColor = [UIColor colorWithString:@"#999999"];
    self.tipsLab.font = [UIFont systemFontOfSize:14];
    self.tipsLab.hidden = YES;
    [self.view addSubview:self.tipsLab];
    [self.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.icon.mas_bottom).offset(20);
    }];
    
    [self refresh];

}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - action
//设置语言
- (void)setLanguageWithlanguage:(NSInteger)language {
    [NSUserDefaults jk_setObject:@(language) forKey:SET_LANGUAGE];
}

#pragma mark - refresh
- (void)refresh{
    self.tipsLab.hidden = self.dataArray.count?YES:NO;
    self.icon.hidden = self.dataArray.count?YES:NO;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger count = 1;
    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = self.dataArray.count;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ARTDeviceListCell *cell = [ARTDeviceListCell cellWithTableView:tableView];
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dataDic = dic;
    cell.delegate = self;
    return cell;
//    LanguageTableViewCell *cell = (LanguageTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:[LanguageTableViewCell jk_className]];
//    if (!cell) {
//        cell = [[LanguageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
//                                          reuseIdentifier:[LanguageTableViewCell jk_className]];
//    }
//    NSDictionary *dict = self.dataArray[indexPath.row];
//    NSString *uuid = [dict valueForKey:@"mac"];
//    NSString *defaultDeviceUUID = [NSUserDefaults jk_stringForKey:ART_Storage_DefaultDeviceUUID];
//    BOOL choosed = NO;
//    if ([uuid isEqualToString:defaultDeviceUUID]) {
//        choosed = YES;
//    }
//    NSString *name = [dict valueForKey:@"name"];
//    NSMutableAttributedString *nameAttribute = [[NSMutableAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:J_ColorFromHEX(0x222222),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
//    NSMutableAttributedString *macAttribute = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"  %@",uuid] attributes:@{NSForegroundColorAttributeName:J_ColorFromHEX(0x666666),NSFontAttributeName:[UIFont systemFontOfSize:13]}];
//    [nameAttribute appendAttributedString:macAttribute];
//    [cell configCellWithAttributeString:nameAttribute isChoose:choosed];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSDictionary *dict = self.dataArray[indexPath.row];
//    NSString *uuid = [dict valueForKey:@"mac"];
//    [NSUserDefaults jk_setObject:uuid forKey:ART_Storage_DefaultDeviceUUID];
//    [self refresh];
//}

- (void)deviceListCell:(ARTDeviceListCell *)cell didClickedSetting:(NSDictionary *)dataDic{
    if (ARTSingleShare.currentPeripheral.state != CBPeripheralStateConnected) {
        [QMUITips showWithText:FYLocalizedString(@"sleep_watch_please_connect", @"")];
        return;
    }
    NSNumber *version = J_UserDefault_GetObjectForKey(ART_Storage_WatchVersion);
    if (version.integerValue == 0 || version.integerValue <= 112) {
        [QMUITips showWithText:FYLocalizedString(@"sleep_watch_firmware", @"")];
        return;
    }
    NSString *name = [dataDic j_stringForKey:@"name"];
    if ([Tool deviceTypeWithName:name] == ARTDeviceTypeWatch) {
        ARTWatchSettingController *controller = [[ARTWatchSettingController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
